﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDSBDemandControl.Presenter;
using System.Web.UI.HtmlControls;
using CDSBDemandControl.Configuration;
using CDSBDemandControl.Model;

namespace CDSBDemandControl.View.Web.PaginasInternas
{
    public partial class CodeReview : System.Web.UI.Page, IDiscriminatorDemand
    {
        private Presenter.DiscriminatorDemand Presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetExpires(DateTime.Now.Subtract(new TimeSpan(24, 0, 0)));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            if (!this.IsPostBack || this.Session["Presenter"] == null)
            {
                this.Presenter = new DiscriminatorDemand(this);

                this.Session["Presenter"] = this.Presenter;

                if (!this.IsPostBack) this.ListarCodeReview(); ;
            }
            else
            {
                this.Presenter = (Presenter.DiscriminatorDemand)this.Session["Presenter"];
                this.Presenter.ResetView(this);
            }
        }

        private void ListarCodeReview()
        {
            this.Presenter.ListWitWithCodeReview();
        }

        public void ExibirCodeReview(IEnumerable<DemandModel> WorkItemsWithCodeReview, IEnumerable<DemandModel> WorkItemsWithoutCodeReview)
        {
            // Binding
            this.GrdCodeReview.DataSource = WorkItemsWithCodeReview;
            this.GrdCodeReview.DataBind();
            this.GrdCodigoAprovado.DataSource = WorkItemsWithoutCodeReview;
            this.GrdCodigoAprovado.DataBind();

            string Msg1 = GrdCodeReview.Rows.Count > 0 ? string.Empty : "Não há WorkItems reprovados no Code Review.";
            string Msg2 = GrdCodigoAprovado.Rows.Count > 0 ? string.Empty : "Não há WorkItems aprovados no Code Review.";

            // Exibir mensagem caso não haja resultados na pesquisa.
            this.ValidarGridSemResultado(Msg1, Msg2);

            // Exibe número total de registros
            lblTotalRegistros1.Text = AgruparTasksPorLider(WorkItemsWithCodeReview, true);
            divTotalRegGrd01.Visible = lblTotalRegistros1.Text.Length > 0 ? true : false;

            lblTotalRegistros2.Text = AgruparTasksPorLider(WorkItemsWithoutCodeReview, false);
            divTotalRegGrd02.Visible = lblTotalRegistros2.Text.Length > 0 ? true : false;

            // Insere texto de observação
            this.ExibirObservacao(string.Format("Tasks no período de {0} à {1}.", ConfigManager.GetPeriodStartDate().ToBrazilianShortDateForUSAShortDate(), DateTime.Now.Date.ToShortDateString()));

            // Exibe a data da última atualização
            (this.Master.FindControl("LblUltimoUpdate") as Label).Text = "Última atualização em: " + this.Presenter.DemandListCodeReviewLastUpdate.ToShortDateString() + " as " + this.Presenter.DemandListCodeReviewLastUpdate.ToShortTimeString();

            (this.Master.FindControl("txtTimeToCompare") as HtmlGenericControl).InnerHtml =
                this.Presenter.DemandListLastUpdate.Year + "," +
                (this.Presenter.DemandListLastUpdate.Month - 1) + "," +
                this.Presenter.DemandListLastUpdate.Day + "," +
                this.Presenter.DemandListLastUpdate.Hour + "," +
                this.Presenter.DemandListLastUpdate.Minute + "," +
                this.Presenter.DemandListLastUpdate.Second;
        }

        private void ValidarGridSemResultado(string msgSemResultado1, string msgSemResultado2)
        {
            this.DivMsgSemResultados1.Visible = (msgSemResultado1.Length > 0);
            this.lblMsgSemResultados1.Text = msgSemResultado1;

            this.DivMsgSemResultados2.Visible = (msgSemResultado2.Length > 0);
            this.lblMsgSemResultados2.Text = msgSemResultado2;
        }

        private void ExibirObservacao(string obs)
        {
            this.LblObsOrdenacao.Text = obs;
            this.LblObsOrdenacao.Visible = this.LblObsOrdenacao.Text.Length > 0 ? true : false;
        }

        private string AgruparTasksPorLider(IEnumerable<DemandModel> Demandas, bool isCodeReviewCollection)
        {
            if (Demandas.Count() < 1) return string.Empty;

            string Texto = "Agrupado por Líder: <br/>";

            IEnumerable<DeveloperModel> Lideres = from Developer in this.Presenter.Developers
                                                  where
                                                        Developer.Mission == Mission.Leader && Developer.DeveloperTeam.IndexOf("TDI") > -1
                                                  orderby
                                                        isCodeReviewCollection ? Developer.TasksDisapprovedInCodeReview :
                                                        (Developer.TasksImplemented - Developer.TasksDisapprovedInCodeReview) descending
                                                  select
                                                        Developer;

            foreach (DeveloperModel Lider in Lideres)
            {
                if (isCodeReviewCollection)
                {
                    Texto += Lider.DeveloperName + " responde por " + Lider.TasksDisapprovedInCodeReview.ToString() + " tarefas;<br>";
                }
                else
                {
                    Texto += Lider.DeveloperName + " responde por " + (Lider.TasksImplemented - Lider.TasksDisapprovedInCodeReview).ToString() + " tarefas;<br>";
                }
            }

            if (Lideres.ToList().Count == 0) return string.Empty;

            return Texto.Substring(0, (Texto.Length - 5)) + ".";
        }

        protected void Grid_DataBinding(object sender, EventArgs e)
        {
            ((GridView)sender).Visible = true;
        }

        #region IDiscriminatorDemand Members

        void IDiscriminatorDemand.ExibirFila(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirEsteira(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirRanking(IEnumerable<CDSBDemandControl.Model.DeveloperModel> developers, IEnumerable<CDSBDemandControl.Model.DeveloperModel> leaders, IEnumerable<CDSBDemandControl.Model.DeveloperModel> focalpoints)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirAtrasos(IEnumerable<CDSBDemandControl.Model.DemandModel> PendingDemands, IEnumerable<CDSBDemandControl.Model.DemandModel> Concludeddemands)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirFaturamento(List<CDSBDemandControl.Model.BillingModel> billing, bool canSeeBilling)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirBugsdeProducao(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirListaDisp(IEnumerable<CDSBDemandControl.Model.DemandModel> list, IEnumerable<CDSBDemandControl.Model.DeveloperModel> modelAusencias, List<DateTime> fer)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirBugsdeProducaoUltimoMes(List<CDSBDemandControl.Model.DemandModel> BugList)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirFactoryWaitingList(List<CDSBDemandControl.Model.BillingModel> Billing, bool AllowsShow)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirAlocacao(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirFactoryProductionList(List<BillingModel> billing, bool canSeeBilling)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirSolicitacoesSites(List<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirManagementTeamTasks(List<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirScenariosAndTasks(List<CDSBDemandControl.Model.ScenarioModel> scenarios)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
